﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NCKH.Personal
{
    public partial class KEHOACHNGHIENCUU : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                hiendetai();
        }

        private void hiendetai()
        {
            string sqlSelect = "SELECT MaDT,TenDT FROM DETAI ";
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cnn.Open();
                    using (SqlDataReader Rd = Cmd.ExecuteReader())
                    {
                        ddrtendt.Items.Insert(0, "-- Chọn tên đề tài --");
                        while (Rd.Read())
                        {
                            ddrtendt.Items.Add(
                                new ListItem(Rd["TenDT"].ToString(),
                                    Rd["MaDT"].ToString()));
                        }
                    }
                    Cnn.Close();
                }
            }
        }
        protected void tenSP_Insert()
        {
            string connection = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connection))
            {
                using (SqlCommand cmd = new SqlCommand("SANPHAM_INSERT", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@TenSP", txttensp.Text);
                     cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                }
            }
        }
        protected void tiendo_Insert()
        {
            string connection = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connection))
            {
                using (SqlCommand cmd = new SqlCommand("BACTD_INSERT", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Tiendo", txttiendo.Text);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                }
            }
        }
        protected void KHNC_Insert()
        {
            string connection = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connection))
            {
                using (SqlCommand cmd = new SqlCommand("KHNC_INSERT", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Thoidiem", txtthoidiem.Text);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                }
            }
        }
        protected void btnok_Click(object sender, EventArgs e)
        {
            tiendo_Insert();
            KHNC_Insert();
            tenSP_Insert();
            thongbao("Bạn đã nhập thành công");
        }
        private void thongbao(string Noidung)
        {

            ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdateCom", "alert('" + Noidung + "');return false;", true);
        }
        
        protected void btnhuy_Click(object sender, EventArgs e)
        {
            txttiendo.Text = txttensp.Text = txtthoidiem.Text = string.Empty;
        }
    }
}